package org.openkanban.client.ui.footer;

import org.openkanban.client.datasource.BoardContactDS;
import org.openkanban.client.datasource.BoardHistoryDS;
import org.openkanban.client.util.RootPanel;

import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.VStack;

/**
 * GUI Element of the Footer.
 */
public class Footer extends VLayout {

    private static final int STACK_HEIGHT = 160;
    public static final int TITLE_HEIGHT = 16;
    public static final int TOTAL_HEIGHT = TITLE_HEIGHT + STACK_HEIGHT;

    private HLayout footerLayout;
    private FooterPanel footerPanel;
    private VStack boardContactStack;
    private VStack boardHistoryStack;
    private VStack userContactsStack;
    private ListGrid userContactsListGrid;
    private ListGrid boardHistoryListGrid;
    private BoardContactTileGrid boardContactsTileGrid;
    private Label noBoardContactsLabel;
    private Label noHistoryDataLabel;

    public Footer() {
	footerLayout = new HLayout();
	footerLayout.setVisible(false);
	footerLayout.setAutoHeight();

	noBoardContactsLabel = new NoBoardNameLabel(146);
	noHistoryDataLabel = new NoBoardNameLabel(146);

	// Board Contacts
	boardContactStack = new BoardContactStack(STACK_HEIGHT, TITLE_HEIGHT);
	boardContactsTileGrid = new BoardContactTileGrid();
	boardContactsTileGrid.setVisible(false);
	boardContactStack.addMember(boardContactsTileGrid);
	boardContactStack.addMember(noBoardContactsLabel);

	// User Contacts
	userContactsStack = new UserContactsStack(STACK_HEIGHT, TITLE_HEIGHT);
	userContactsListGrid = new UserContactsListGrid();
	userContactsStack.addMember(userContactsListGrid);

	// Board History
	boardHistoryStack = new BoardHistoryStack(STACK_HEIGHT, TITLE_HEIGHT);
	boardHistoryListGrid = new HistoryListGrid();
	boardHistoryListGrid.setVisible(false);
	boardHistoryStack.addMember(boardHistoryListGrid);
	boardHistoryStack.addMember(noHistoryDataLabel);

	// Adding
	footerLayout.addMember(boardContactStack);
	footerLayout.addMember(userContactsStack);
	footerLayout.addMember(boardHistoryStack);

	setBackgroundColor("#888888");
	setAutoHeight();
	setWidth100();

	footerPanel = new FooterPanel(this);

	addMember(footerPanel);
	addMember(footerLayout);

	moveTo(0, getTopPosition());
    }

    public int getTopPosition() {
	if (footerPanel.isOpened()) {
	    return RootPanel.main.getVlMain().getHeight() - TOTAL_HEIGHT - 4;
	} else {
	    return RootPanel.main.getVlMain().getHeight() - TITLE_HEIGHT - 4;
	}
    }

    public HLayout getFooterLayout() {
	return footerLayout;
    }

    public void setFooterLayout(HLayout footerLayout) {
	this.footerLayout = footerLayout;
    }

    public VStack getBoardContactStack() {
	return boardContactStack;
    }

    public FooterPanel getFooterPanel() {
	return footerPanel;
    }

    public void showBoardContactTileGrid() {
	if (boardContactStack.contains(noBoardContactsLabel)) {
	    boardContactStack.removeMember(noBoardContactsLabel);
	    boardContactsTileGrid.setDataSource(BoardContactDS.getInstance(RootPanel.getCurrentBoardDto().getBoardId().toString()));
	    boardContactsTileGrid.fetchData();

	    boardContactsTileGrid.show();
	}
    }

    public void hideBoardContactTileGrid() {
	if (!boardContactStack.contains(noBoardContactsLabel)) {
	    boardContactStack.addMember(noBoardContactsLabel);
	    if (boardContactsTileGrid.getDataSource() != null) {
		boardContactsTileGrid.getDataSource().destroy();
	    }
	    BoardContactDS.setInstanceNull();
	    boardContactsTileGrid.hide();
	}
    }

    public void showBoardHistoryListGrid() {
	if (boardHistoryStack.contains(noHistoryDataLabel)) {
	    boardHistoryStack.removeMember(noHistoryDataLabel);
	    boardHistoryListGrid.setDataSource(BoardHistoryDS.getInstance(RootPanel.getCurrentBoardDto().getBoardId().toString()));
	    boardHistoryListGrid.fetchData();

	    boardHistoryListGrid.show();
	}
    }

    public void hideBoardHistoryListGrid() {
	if (!boardHistoryStack.contains(noHistoryDataLabel)) {
	    boardHistoryStack.addMember(noHistoryDataLabel);
	    if (boardHistoryListGrid.getDataSource() != null) {
		boardHistoryListGrid.getDataSource().destroy();
	    }
	    BoardHistoryDS.setInstanceNull();
	    boardHistoryListGrid.hide();
	}
    }

    public void updateBoardContactData() {

	boardContactsTileGrid.getDataSource().invalidateCache();
	boardContactsTileGrid.fetchData();
	boardContactsTileGrid.filterData();
	boardContactsTileGrid.redraw();
    }

    public void updateBoardHistoryData() {
	boardHistoryListGrid.getDataSource().invalidateCache();
	boardHistoryListGrid.fetchData();
	boardHistoryListGrid.filterData();
	boardHistoryListGrid.redraw();
    }

    public void updateUserContactsData() {
	userContactsListGrid.refreshFields();
	userContactsListGrid.fetchData();
	userContactsListGrid.filterData();
	userContactsListGrid.redraw();
	userContactsListGrid.refreshFields();
    }

    public void refreshTopPosition() {
	setTop(getTopPosition());
    }

}
